package com.example.wc.logindemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

/**
 * Created by hui on 2017/2/17.
 */
public class BlogsListFragment extends Fragment {

    private View fragmentView;

//    private TextView mBack;
    private ImageButton mWriteImageButton;
    private RecyclerView mBlogsRecyclerView;

    private BlogAdapter mAdapter;

    private BlogLab.Content responseSave;
    private List<Blog> mBlogs;

    private VolleyRequestDemo mVolleyRequestDemo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        if (fragmentView != null) {
            return fragmentView;
        }
        fragmentView = inflater.inflate(R.layout.fragment_blogs_list, container, false);

//        mBack = (TextView) fragmentView.findViewById(R.id.back_title_homepage);
//        mBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(BlogsListFragment.this.getContext(), LoginDemoActivity.class);
//                startActivity(intent);
//            }
//        });

        mWriteImageButton = (ImageButton) fragmentView.findViewById(R.id.write_image_button);
        mWriteImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(BlogsListFragment.this.getContext(), WriteBlogActivity.class);
                startActivity(i);
            }
        });

        mBlogsRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.blogs_recycler_view);
        mBlogsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        if(mVolleyRequestDemo == null){
            mVolleyRequestDemo = new VolleyRequestDemo();
        }
        mVolleyRequestDemo.volleyRequestDemo_GET(getContext(), new VolleyRequestDemo.BlogGetListener() {
            @Override
            public void onBlogGeted(BlogLab.Content content) {
                //这里是子线程，子线程还未结束，调用了更新界面
                responseSave = content;
                Log.d("GET_TAG", responseSave.toString());
                mBlogs = responseSave.content;
                updateUI();
            }
        });

        return fragmentView;
    }

    private void updateUI() {
        if(mAdapter == null){
            mAdapter = new BlogAdapter(BlogsListFragment.this.getContext(), mBlogs);
            mBlogsRecyclerView.setAdapter(mAdapter);
//            ItemTouchHelper.Callback callback=new RecycleItemTouchHelper(mAdapter);

            mAdapter.MyItemTouchHelper(mBlogsRecyclerView);

//            ItemTouchHelper.Callback callback=new ItemTouchHelper.Callback(){
//
//                @Override
//                public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//                    return makeMovementFlags(ItemTouchHelper.UP|ItemTouchHelper.DOWN,ItemTouchHelper.START );
//                }
//
//                @Override
//                public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//                    return true;
//                }
//
//                @Override
//                public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//
//                }
//            };
//            ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
//            itemTouchHelper.attachToRecyclerView(mBlogsRecyclerView);
        }else{
            mAdapter.setBlogs(mBlogs);
            mAdapter.notifyDataSetChanged();
        }
    }
}
